package models;

import java.util.Calendar;
import java.util.Date;

import org.joda.time.DateMidnight;
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.Hours;
import org.joda.time.Seconds;

import play.Play;

public class TimeUtils {

	public static int HOUR_TO_MILLISECONDS = 3600000;

	public static int daysBetween(Date startDate, Date endDate) {
		Days days = Days.daysBetween(new DateTime(startDate), new DateTime(
				endDate));
		return days.getDays();
	}

	public static int hoursBetween(Date start, Date end) {
		Hours hours = Hours
				.hoursBetween(new DateTime(start), new DateTime(end));
		return hours.getHours();
	}

	public static int howManyDaysFromNow(Date startDate) {
		Days days = Days.daysBetween(new DateTime(startDate), DateTime.now());
		return days.getDays();
	}

	public static int secondsBetween(Date startDate, Date endDate) {
		Seconds seconds = Seconds.secondsBetween(new DateTime(startDate),
				new DateTime(endDate));

		return seconds.getSeconds();
	}

	public static boolean isToday(Date dDate) {
		Days days = Days.daysBetween(new DateTime(dDate), DateTime.now());
		return days.getDays() == 0;
	}

	public static Calendar dateToCalendar(Date when) {
		Calendar now = Calendar.getInstance();
		now.setTime(when);
		return now;
	}

	public static Long getWorkHoursInMilis() {
		float hours = Float.parseFloat((String) Play.configuration
				.get("workday.hours"));
		return (long) ((hours * HOUR_TO_MILLISECONDS) + 0L);
	}

	public static Long hoursToMiliseconds(int hours) {
		return new Float(hours * HOUR_TO_MILLISECONDS).longValue();
	}

	public static Date toMidnight(Date when) {
		DateMidnight midnight = new DateMidnight(new DateTime(when));
		return midnight.toDate();
	}
}
